package com.example.smartf.repository.mongoDB.Impl;

import com.example.smartf.model.mongo.Group;
import com.example.smartf.repository.mongoDB.MongoGroupUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MongoGropUtilImpl implements MongoGroupUtil {
    //日志记录器
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MongoTemplate mongoTemplate;

    /*查找所有Group信息*/
    public List<Group> findAll(){
        return mongoTemplate.findAll(Group.class,"Group");
    }

    /*查找一个Group*/
    public Group findByGroupID(String groupID){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        return mongoTemplate.findOne(query,Group.class,"Group");
    }

    /*按照username查找一个group*/
    public Group findGroupByusername(String username){
        Query query = new Query(Criteria.where("users").is(username));
        return mongoTemplate.findOne(query,Group.class,"Group");
    }


    /*按照deviceID查找一个group*/
    public Group findGroupBydeviceID(String deviceID){
        Query query = new Query(Criteria.where("devices").is(deviceID));
        return mongoTemplate.findOne(query,Group.class,"Group");
    }

    /*username是否在Group 中*/
    public Boolean userinGroup(String groupID, String username){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        query.addCriteria(Criteria.where("users").is(username));
        if( null == mongoTemplate.findOne(query,Group.class,"Group")){
            return false;
        }else{
            return true;
        }
    }


    /*deviceID是否在Group中*/
    public Boolean devinGroup(String groupID, String deviceID){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        query.addCriteria(Criteria.where("devices").is(deviceID));
        if( null == mongoTemplate.findOne(query,Group.class,"Group")){
            return false;
        }else{
            return true;
        }
    }


    /*增加一个 新的group */
    public Group addGroup(Group group){
        return mongoTemplate.save(group,"Group");
    }
    /*删除一个 group*/
    public void delGroup(String groupID){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        mongoTemplate.remove(query,"Group");
    }



    /*更新group下的设备*/
    public void updateGroupDevices(String groupID , String devices[]){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        Update update = new Update().set("devices",devices);
        mongoTemplate.updateMulti(query,update,Group.class,"Group");

    }

    /*更新group下的用户*/
    public void updateGroupUsers(String groupID , String users[]){
        Query query = new Query(Criteria.where("groupID").is(groupID));
        Update update = new Update().set("users",users);
        mongoTemplate.updateMulti(query,update,Group.class,"Group");
    }

}
